<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

class CreateOrders extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {

            $table->unsignedBigInteger('orderId');
            $table->unsignedInteger("brandId")->default(0)->comment("品牌 Id");
            $table->unsignedInteger("shopId")->default(0)->comment("店铺 ID");
            $table->unsignedInteger("userId")->default(0)->comment("用户 ID");
            $table->tinyInteger("sourceType")->default(1)->comment("订单来源 1 pos 2小程序 3美团外卖 4 饿了么 ");
            $table->integer("serialNum")->default(0)->comment("流水号");
            $table->integer("originalPrice")->default(0)->comment("原价");
            $table->integer("shouldPrice")->default(0)->comment("应收");
            $table->integer("discountPrice")->default(0)->comment("优惠价格");
            $table->integer("couponPrice")->default(0)->comment("优惠券优惠的价格");
            $table->integer("payPrice")->default(0)->comment("实收");
            $table->integer("completeTime")->default(0)->comment("订单完成时间");
            $table->tinyInteger("orderStatus")->default(0)->comment("订单状态 0 待支付 1 待制作 2 待取餐 3 已完成");
            $table->tinyInteger("payStatus")->default(0)->comment("支付状态 0 待支付 1 已支付");
            $table->tinyInteger("refundStatus")->default(0)->comment("反结账状态 0 无反结账 1 部分反结账 2 全额反结账");
            $table->string("remark", 255)->default('')->comment("订单备注");

            // 统一表字段
            $table->integer('createdAt')->default(0)->comment('创建时间');
            $table->integer('createdByUid')->default(0)->comment('创建人');
            $table->integer('updatedAt')->default(0)->comment('修改时间');
            $table->integer('updatedByUid')->default(0)->comment('修改人');
            $table->string('updatedBy')->default(0)->comment('修改人');
            $table->string('createdBy')->default(0)->comment('创建人');

            // 索引部分
            $table->primary('orderId', 'idx_orderId');        // 主键非自增
            $table->index(['orderId', 'brandId', 'shopId'], 'idx_orderId_brandId_shopId');  // 索引
            $table->index('createdAt', 'idx_createdAt');

            $table->collation = 'utf8mb4_general_ci';
        });

        $prefix = config('other.DB_PREFIX', '');
        \Illuminate\Support\Facades\DB::statement("ALTER TABLE `{$prefix}orders` COMMENT = '订单表'"); //表注释
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
}
